package com.gsafety.libraryktx.util

import android.content.Context
import android.graphics.Color
import android.graphics.Matrix
import com.github.mikephil.charting.charts.BarChart
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.components.YAxis
import com.github.mikephil.charting.data.BarData
import com.github.mikephil.charting.data.BarDataSet
import com.github.mikephil.charting.data.BarEntry
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet
import com.gsafety.libraryktx.R
import com.gsafety.libraryktx.bean.AxisText

/**
 * @author : lj
 * @date : 2020/7/30 17:12
 */
class BarChartManager(barChart: BarChart) {
    var mBarChart: BarChart? = null
    var leftAxis: YAxis? = null
    var rightAxis: YAxis? = null
    var xAxis: XAxis? = null

    init {
        mBarChart = barChart
        leftAxis = barChart.axisLeft
        rightAxis = barChart.axisRight
        xAxis = barChart.xAxis
    }

    private fun initChart(context: Context) {
        mBarChart?.let {
            it.setScaleEnabled(false)
            it.setBackgroundColor(context.resources.getColor(R.color.color_white))
            it.setDrawBarShadow(false)
            it.setDrawGridBackground(false)
            it.isHighlightFullBarEnabled = false
            //不显示边界
            it.setDrawBorders(false)
            it.description.isEnabled = false
            it.legend.isEnabled = false

            //XY轴的设置
            xAxis!!.position = XAxis.XAxisPosition.BOTTOM//X轴设置显示位置在底部
            xAxis!!.granularity = 1f//设置最小间隔，防止当放大时，出现重复标签
            xAxis!!.textColor = Color.GRAY//设置X轴字体颜色
            xAxis!!.textSize = 10f //设置X轴字体大小
            xAxis!!.setDrawGridLines(false) //不绘制X轴网格，默认为绘制。
            //y轴设置
            leftAxis!!.axisMinimum = 0f //保证Y轴从0开始，不然会上移一点
            leftAxis!!.setDrawGridLines(false)

            rightAxis!!.axisMinimum = 0f //保证Y轴从0开始，不然会上移一点
            rightAxis!!.enableGridDashedLine(10f,10f,0f)
            //将条目放大 可滑动
            it.invalidate()
            var mMatrix = Matrix();
            mMatrix.postScale(1f, 1f); //X轴宽度放大2倍 竖直方向不变
            it.viewPortHandler.refresh(mMatrix, mBarChart, false);
        }
    }


    /**
     * 设置X轴的值
     *
     * @param max
     * @param min
     * @param labelCount
     */
    fun setXAxis(max: Float, min: Float, labelCount: Int,force:Boolean) {
        xAxis!!.axisMaximum = max
        xAxis!!.axisMinimum = min
        xAxis!!.setLabelCount(labelCount, force)
        mBarChart!!.invalidate()
    }

    //展示柱状图单条
    fun showBarChart(context: Context, xAxisValues: MutableList<AxisText>, yAxisValues: MutableList<Float>, color: MutableList<Int>) {
        initChart(context)//首先进项基本数据的初始化
        var entries = ArrayList<BarEntry>()//将数据源添加到图标

        var empty = mutableListOf<String>()
        xAxisValues.forEachIndexed { index, it ->
            entries.add(BarEntry(it.code!!, yAxisValues[index]))
            empty.add(it.name!!)
        }
        xAxis!!.valueFormatter=IndexAxisValueFormatter(empty)
        var barDataSet = BarDataSet(entries, "")
        barDataSet.colors = color //摄住柱状图颜色
        barDataSet.valueTextSize = 9f //设置数值字体大小
        barDataSet.formLineWidth = 1f //线条宽度
        barDataSet.formSize = 15f ///图例窗体的大小
        var dataSets = mutableListOf<IBarDataSet>()
        dataSets.add(barDataSet)
        var data = BarData(dataSets)
        data.barWidth = 0.3f
        mBarChart!!.data = data

    }

    //多条柱状图
    fun showMoreBarChart(context: Context, xAxisValues: MutableList<AxisText>, yAxisValues: MutableList<List<Float>>, color: MutableList<Int>) {
        initChart(context)//首先进项基本数据的初始化
        var data = BarData()

        var empty = mutableListOf<String>()
        xAxisValues.forEach {
           empty.add(it.name!!)
        }
        xAxis!!.valueFormatter=IndexAxisValueFormatter(empty)
        xAxis!!.setCenterAxisLabels(true)//设置X轴文字剧中对齐
        yAxisValues.forEachIndexed { i, it ->
            var entries = ArrayList<BarEntry>()//将数据源添加到图标
            it.forEachIndexed { j, fl ->
                entries.add(BarEntry(xAxisValues[j].code!!, fl))
            }
            var barDataSet = BarDataSet(entries, "")
            barDataSet.color = color[i] //摄住柱状图颜色
            barDataSet.valueTextSize = 9f //设置数值字体大小
            barDataSet.formLineWidth = 1f //线条宽度
            barDataSet.formSize = 15f ///图例窗体的大小
            data.addDataSet(barDataSet)


        }
        var amount = yAxisValues.size
        var groupSpace = 0.5f//柱状图组之间的间距
        var barSpace = ((1 - 0.5f) / amount / 10).toFloat()// x4 DataSet
        var barWidth = ((1 - 0.5f) / amount / 10 * 9).toFloat() // x4 DataSet
        data.barWidth = barWidth
        data.groupBars(0f, groupSpace, barSpace);
        mBarChart!!.data=data

    }



}